<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
    Author : Christian Hadiwinoto 13507081
*/
include 'page/abstractPage.php';

//1. Nama kelas samakan dengan nama file - WAJIB extend AbstractPage
class ServerMengelolaDataRuangan extends AbstractPage {
    
    private $data;
    private $action;    //keterangan aksi dari button submit submit (isi sendiri dengan string)
    private $message;   //keterangan pesan berhasil atau sukses

    public function ServerMengelolaDataRuangan()
    {
        parent::__construct();

        // Model/Data Terkait
        $this->load->model('Ruangan');
        $this->load->model('Pengguna');

        // Session Pengguna dan Redirect jika tidak ada privilege
        $sess_user = $this->session->userdata('pengguna');
        if (!array_intersect($sess_user['role'], array("TU"))) {
            redirect('/');
        }
    }

    public function indexAdministrasiRuangan() {
        //definisi action
        $this->action='index';

        //proses
        //Pembuatan Template Tabel
        $tmpl = array (
                    'table_open'          => '<table border="1" cellpadding="4" cellspacing="0" class="table1">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
        $hasil = $this->Ruangan->getAllRuangan();
        $this->table->set_heading(
                'ID',
                'Nama Ruangan',
                'Deskripsi',
                'Penanggung Jawab',
                'Subruangan dari',
                'Edit','Hapus'
                );
        foreach ($hasil as $row) {
            $fattr = array('name' => 'formHapus'.$row->roomID,
                'id' => 'formHapus'.$row->roomID);
            $linkEdit = 'ServerMengelolaDataRuangan/formEditRuangan/' .
                    $row->roomID;
            $linkHapus = 'ServerMengelolaDataRuangan/deleteRuangan/' .
                    $row->roomID;
            $pj = $this->Pengguna->getDataPengguna($row->pjRuangan);
            $namapj = $pj->name;
            $parentName = $row->parentRoom ?
                    $this->Ruangan->getRuangan($row->parentRoom)->roomName :
                    '-';
            $this->table->add_row($row->roomID,
                    $row->roomName,
                    $row->description ? $row->description : '-',
                    $namapj." (".$row->pjRuangan.")",
                    $parentName,
                    '<a onclick="window.location = \''.site_url($linkEdit).'\';">Edit</a>',
                    '<a onclick="confirmDelete(\''.$row->roomName.'\',\''.site_url($linkHapus).'\');">Hapus</a>');
        }
        $this->tabelRuangan = $this->table->generate();
        $this->linkTambahRuangan = anchor('ServerMengelolaDataRuangan/formAddRuangan',
                'Tambah Ruangan');
        
        //eksekusi: load ke konten
        $this->index();
    }

    public function addRuangan() {
        //nama action
        $this->action = 'add';

        //proses
        $roomId = $this->input->post('roomId');
        $this->Ruangan->setRoomId($roomId);
        $roomName = $this->input->post('roomName');
        $this->Ruangan->setRoomName($roomName);
        $description = $this->input->post('description');
        $this->Ruangan->setDescription($description);
        $pjRuangan = $this->input->post('pjRuangan');
        $this->Ruangan->setPjRuangan($pjRuangan);
        $parentRoom = $this->input->post('parentRoom');
        $this->Ruangan->setParentRoom($parentRoom);
        $this->Ruangan->insertRuangan();
        
        //eksekusi: load ke konten
        redirect('ServerMengelolaDataRuangan/indexAdministrasiRuangan');
    }

    public function deleteRuangan($roomId) {
        //nama action
        $this->action = 'delete';

        //proses
        $this->Ruangan->setRoomId($roomId);
        $this->Ruangan->deleteRuangan();

        //eksekusi: load ke konten
        redirect('ServerMengelolaDataRuangan/indexAdministrasiRuangan');
    }

    public function editRuangan() {
        //nama action
        $this->action = 'edit';

        //proses
        $roomId = $this->input->post('roomId');
        $edRoomId = $this->input->post('edRoomId');
        $roomName = $this->input->post('roomName');
        $description = $this->input->post('description');
        $pjRuangan = $this->input->post('pjRuangan');
        $parentRoom = $this->input->post('parentRoom');
        
        $this->Ruangan->setRoomId($roomId);

        $ruanganbaru = array(
            'roomID' => $edRoomId,
            'roomName' => $roomName,
            'description' => $description,
            'pjRuangan' => $pjRuangan,
            'parentRoom' => $parentRoom
            );
        $this->Ruangan->changeRuangan($ruanganbaru);

        //eksekusi: load ke konten
        redirect('ServerMengelolaDataRuangan/indexAdministrasiRuangan');
    }

    //Fungsi untuk menambah ruangan
    public function formAddRuangan()
    {
        //nama action
        $this->action = 'formAdd';

        //proses
        $tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="1" cellspacing="0">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
        $this->load->helper('form');
        $fattr = array('name' => 'formAddRuangan',
            'id' => 'formAddRuangan'
            );
        $form = form_open('ServerMengelolaDataRuangan/addRuangan',$fattr);
        $field = array(
              'name'        => 'roomId',
              'id'          => 'roomId',
              'maxlength'   => '10',
              'size'        => '5'
            );
        $this->table->add_row(form_label('ID','roomId'),
                form_input($field));
        $field = array(
              'name'        => 'roomName',
              'id'          => 'roomName',
              'maxlength'   => '30',
              'size'        => '15'
            );
        $this->table->add_row(form_label('Nama','roomName'),
                form_input($field));
        $field = array(
              'name'    => 'description',
              'id'      => 'description',
              'rows'    => '3',
              'cols'    => '30'
            );
        $this->table->add_row(form_label('Deskripsi','description'),
                form_textarea($field));
        $field = array(
              'name'        => 'pjRuangan',
              'id'          => 'pjRuangan',
              'maxlength'   => '20',
              'size'        => '10'
            );
        $options = array();
        $hasil = $this->Pengguna->getAllPengguna();
        $options[""] = "-- pilih pengguna --";
        foreach ($hasil as $row) {
            $options[$row->userID] = $row->userID . " - " . $row->name;
        }
        $this->table->add_row(form_label('Penanggung jawab','pjRuangan'),
                form_dropdown('pjRuangan',$options,'','id = "pjRuangan"'));
        $options = array();
        $hasil = $this->Ruangan->getAllRuangan();
        $options[""] = "(berdiri sendiri)";
        foreach ($hasil as $row) {
            $options[$row->roomID] = $row->roomName . " (PJ: ".
                    $this->Pengguna->getDataPengguna($row->pjRuangan)->name .")";
        }
        $this->table->add_row(form_label('Subruangan dari','parentRoom'),
                form_dropdown('parentRoom',$options,'','id = "parentRoom"'));
        $js = 'onclick="window.location = \''.
            site_url('ServerMengelolaDataRuangan/indexAdministrasiRuangan') .'\';"';
        $tabdata = array(
            'data' => form_submit('submitAddRuangan',
                    'Tambah','id="submitAddRuangan" class="f-submit" ')."&nbsp".
                form_button('cancelAddRuangan','Batal',$js.' class="f-submit"'),
            'colspan' => '2'
            );
        $this->table->add_row($tabdata);
        $form .= $this->table->generate();
        $form .= form_close();
        $this->formTambah = $form;
        
        //load konten
        $this->index();
    }

    //Fungsi untuk menambah ruangan
    public function formEditRuangan($roomId)
    {
        //nama action
        $this->action = 'formEdit';

        //proses
        $tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="1" cellspacing="0">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
        $this->load->helper('form');
        $ruangan = $this->Ruangan->getRuangan($roomId);
        $identifier = array('roomId' => $ruangan->roomID);
        $fattr = array('name' => 'formEditRuangan',
            'id' => 'formEditRuangan');
        $form = form_open('ServerMengelolaDataRuangan/editRuangan', $fattr,
                $identifier);
        $field = array(
              'name'        => 'edRoomId',
              'id'          => 'edRoomId',
              'maxlength'   => '10',
              'size'        => '5',
              'value'       => $ruangan->roomID);
        $this->table->add_row(form_label('ID','roomId'),
                form_input($field));
        $field = array(
              'name'        => 'roomName',
              'id'          => 'roomName',
              'maxlength'   => '30',
              'size'        => '15',
              'value'       => $ruangan->roomName);
        $this->table->add_row(form_label('Nama','roomName'),
                form_input($field));
        $field = array(
              'name'    => 'description',
              'id'      => 'description',
              'rows'    => '3',
              'cols'    => '30',
              'value'   => $ruangan->description);
        $this->table->add_row(form_label('Deskripsi','description'),
                form_textarea($field));
        $field = array(
              'name'        => 'pjRuangan',
              'id'          => 'pjRuangan',
              'maxlength'   => '20',
              'size'        => '10'
            );
        $options = array();
        $hasil = $this->Pengguna->getAllPengguna();
        $options[""] = "-- pilih pengguna --";
        foreach ($hasil as $row) {
            $options[$row->userID] = $row->userID . " - " . $row->name;
        }
        $this->table->add_row(form_label('Penanggung jawab','pjRuangan'),
                form_dropdown('pjRuangan',$options,$ruangan->pjRuangan,'id = "pjRuangan"'));
        $options = array();
        $hasil = $this->Ruangan->getAllRuangan();
        $options[""] = "(berdiri sendiri)";
        foreach ($hasil as $row) {
            $options[$row->roomID] = $row->roomName . " (PJ: ".
                    $this->Pengguna->getDataPengguna($row->pjRuangan)->name .")";
        }
        $this->table->add_row(form_label('Subruangan dari','parentRoom'),
                form_dropdown('parentRoom',$options,$ruangan->parentRoom,'id = "parentRoom"'));
        $js = 'onclick="window.location = \''.
            site_url('ServerMengelolaDataRuangan/indexAdministrasiRuangan') .'\';"';
        $tabdata = array(
            'data' => form_submit('submitEditRuangan',
                    'Simpan','id="submitEditRuangan" class="f-submit" ')."&nbsp".
                form_button('cancelEditRuangan','Batal',$js.' class="f-submit"'),
            'colspan' => '2'
            );
        $this->table->add_row($tabdata);
        $form .= $this->table->generate();
        $form .= form_close();
        $this->formEdit = $form;

        //load konten
        $this->index();
    }

    //5. Wajib Override method ini
    function _buildContent()
    {
        switch($this->action) {
            case('index'):
                /*data yang dikirimkan*/
                $this->data = array('tabelRuangan' => $this->tabelRuangan,
                    'linkTambahRuangan' => $this->linkTambahRuangan);
                $this->load->view("AdministrasiRuangan/IndexAdministrasiRuangan",
                        $this->data);
                break;
            case('formAdd'):
                $this->data = array('formTambah' => $this->formTambah);
                $this->load->view("AdministrasiRuangan/FormTambahRuangan",
                        $this->data);
                break;
            case('formEdit'):
                $this->data = array('formEdit' => $this->formEdit);
                $this->load->view("AdministrasiRuangan/FormEditRuangan",
                        $this->data);
                break;
        }
    }
}
